package com.actimize.webapp.action;

import java.io.IOException;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

public class GetSecurityQuestion extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static final String password= "belgrade";
	
	public String doDummy() throws IOException {
		return SUCCESS;
	}
	
	
    public String getLetters() throws IOException {

    	HttpServletRequest request = getRequest();
    	Random rand = new Random();
        int i = rand.nextInt(password.length());

        getSession().setAttribute("firstNum", i+1);
        
        getSession().setAttribute("firstLetter", password.substring(i, i+1));
        request.setAttribute("firstLetter", password.substring(i, i+1));
        i = rand.nextInt(password.length());
        
        getSession().setAttribute("secondNum", i+1);
        
        getSession().setAttribute("secondLetter", password.substring(i, i+1));
        request.setAttribute("secondLetter", password.substring(i, i+1));
        i = rand.nextInt(password.length());
        
        getSession().setAttribute("thirdNum", i+1);
        
        getSession().setAttribute("thirdLetter", password.substring(i, i+1));
        request.setAttribute("thirdLetter", password.substring(i, i+1));
        
        return SUCCESS;
    }
    
    public String checkQuestion() throws IOException {
        HttpServletRequest request = getRequest();
        System.out.println("Dragan pocetak");
        System.out.println((String)getSession().getAttribute("firstLetter"));
        System.out.println((String)getSession().getAttribute("secondLetter"));
        System.out.println((String)getSession().getAttribute("thirdLetter"));
        
        System.out.println((String)request.getParameter("memInfo1"));
        System.out.println((String)getSession().getAttribute("firstLetter"));
        
        if(!((String)request.getParameter("memInfo1")).endsWith((String)getSession().getAttribute("firstLetter"))){
        	System.out.println("prvi");
        	return ERROR;
        }
        
        if(!((String)request.getParameter("memInfo2")).endsWith((String)getSession().getAttribute("secondLetter"))){
        	System.out.println("drugi");
    	return ERROR;
        }
        
        if(!((String)request.getParameter("memInfo3")).endsWith((String)getSession().getAttribute("thirdLetter"))){
        	System.out.println("treci");
    	return ERROR;
        }

        System.out.println("Dragan kraj");
        return SUCCESS;
    }

	

}
